//
//  NewsViewCell.m
//  phoneTool
//
//  Created by HappyMilk 02 on 2017/7/28.
//  Copyright © 2017年 tomi. All rights reserved.
//

#import "NewsViewCell.h"
#import "UIImageView+WebCache.h"
#import "FLAnimatedImage.h"
#import "LoginHeader.h"

@interface NewsViewCell (){
    
    NSData *_imageData;
    UIImage *_shareImage;
}

@end

@implementation NewsViewCell
    
- (void)shareAction{
    UIViewController * rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    NSString *textToShare = _lab_title.text;
    UIImage * imageToShare = _shareImage;
    //NSURL *urlToShare = [NSURL URLWithString:m_downloadUrl];
    
    NSMutableArray *activityItems = [[NSMutableArray alloc]init];
    
    if (textToShare != nil) {
        [activityItems addObject:textToShare];
    }
    
    if (imageToShare != nil) {
        [activityItems addObject:imageToShare];
    }
    
//    if (urlToShare != nil) {
//        [activityItems addObject:urlToShare];
//    }
    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems
                                                                            applicationActivities:nil];
    
    activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList];
    
    UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(UIActivityType activityType, BOOL completed, NSArray * returnedItems, NSError * activityError){
        
    };
    
    activityVC.completionWithItemsHandler = myBlock;
    
    [rootVc presentViewController:activityVC animated:TRUE completion:nil];
}

- (void)setListDic:(NSDictionary *)listDic{
    _listDic = listDic;
    self.lab_title.text = [listDic objectForKey:@"text"];
    
    [_btn_share addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    
    if ([listDic objectForKey:@"video"] != nil) {
        NSDictionary *dic = [listDic objectForKey:@"video"];
        self.view_gif.hidden = YES;
        self.view_imgv.hidden = YES;
        self.view_video.hidden = NO;
        
        if ([[dic objectForKey:@"thumbnail"] objectAtIndex:0] != nil) {
            [self.imgv_video_thrumb sd_setImageWithURL:[NSURL URLWithString:[[dic objectForKey:@"thumbnail"] objectAtIndex:0]]];
            _shareImage = self.imgv_video_thrumb.image;
        }
        
    }else if ([listDic objectForKey:@"gif"] != nil){
        
        NSDictionary *dic = [listDic objectForKey:@"gif"];
        self.view_gif.hidden = NO;
        self.view_imgv.hidden = YES;
        self.view_video.hidden = YES;
        
        
        if ([[dic objectForKey:@"images"] objectAtIndex:0] != nil) {
            
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                _imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[dic objectForKey:@"images"] objectAtIndex:0]]];
                FLAnimatedImage *flimage = [FLAnimatedImage animatedImageWithGIFData:_imageData];
                
                _shareImage = flimage.posterImage;
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.imgv_gif.animatedImage = flimage;
                });
            });
        }
        
    
    }else if ([listDic objectForKey:@"image"] != nil){
        
        NSDictionary *dic = [listDic objectForKey:@"image"];
        self.view_gif.hidden = YES;
        self.view_imgv.hidden = NO;
        self.view_video.hidden = YES;
        
        if ([[dic objectForKey:@"download_url"] objectAtIndex:0] != nil) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                _imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[dic objectForKey:@"download_url"] objectAtIndex:0]]];
            });
        }
        if ([[dic objectForKey:@"thumbnail_small"] objectAtIndex:0] != nil) {
            [self.imgv_thrumb sd_setImageWithURL:[NSURL URLWithString:[[dic objectForKey:@"thumbnail_small"] objectAtIndex:0]]];
            _shareImage = self.imgv_thrumb.image;
        }
    }
}

- (IBAction)longImageViewAction:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(showContentsWithData:andIsGif:)]) {
        [self.delegate showContentsWithData:_imageData andIsGif:NO];
    }
}
- (IBAction)gifImagevAction:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(showContentsWithData:andIsGif:)]) {
        [self.delegate showContentsWithData:_imageData andIsGif:YES];
    }
}

- (IBAction)videoAction:(UIButton *)sender {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [[AdLibrary stander] showAd];
    NSDictionary *dic = [_listDic objectForKey:@"video"];
    if ([[dic objectForKey:@"download"] objectAtIndex:0] != nil) {
        UIWebView * webView = [[UIWebView alloc]initWithFrame:_imgv_video_thrumb.frame];
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[dic objectForKey:@"download"] objectAtIndex:0]]]];
        webView.scrollView.scrollEnabled = NO;
        [self.imgv_video_thrumb addSubview:webView];
    }
}




- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
